package com.ymatou.paygetway.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.payment.parameter.AcquireOrderBean;
import com.ymatou.paygetway.parameter.CMBSignNotifyBean;
import com.ymatou.paygetway.serivce.CMBSignNotifyCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.PayGateWayCallService;
import com.ymttest.database.model.MCmbaggrement;
import com.ymttest.database.sqlwapper.PayGateWayWapper;
import com.ymttest.utils.TestDataManager;


@TestSuite
public class Ts_CMBSignNotify {
	private static CMBSignNotifyBean cmbsnotifyBean;
	private static CMBSignNotifyCall cmbsnotifyCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("招行签约回调");
	}

	@Before
	public void caseUp() {
		cmbsnotifyBean = new CMBSignNotifyBean();
		cmbsnotifyCall = new CMBSignNotifyCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(cmbsnotifyCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CMBSignNotify_001() {
		Logger.start(true, "招行签约回调返回成功");
		try {
			new PayGateWayWapper().deleteCmbAggrementByUId("1");
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("50");
			aqb.setUserId("1");
			PayGateWayCallService.acquireOrder(aqb);
			cmbsnotifyBean=PayGateWayCallService.initCMBSignNotifyBean(aqb);
			String sign=PayGateWayCallService.SignCMBNoticeData(cmbsnotifyBean.getNoticeData());
			cmbsnotifyBean.setSign(sign);
			String send = PayGateWayCallService.CMBNoticeSendString(cmbsnotifyBean);
//			alinotifyBean = initAliPayNotifyBean(aqb);
//			String send = aliPayNotifyBeanToSend(alinotifyBean,aqb);
			cmbsnotifyCall.setData(send);
			cmbsnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			cmbsnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);
			cmbsnotifyCall.callService();
			Logger.debug(cmbsnotifyCall.getStatusCode());
			Logger.verifyEquals("success", cmbsnotifyCall.getReturnData(), "验证success");
			verifyDB(cmbsnotifyBean,"1");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_CMBSignNotify_002() {
		Logger.start(false, "招行签约回调返回失败");
		try {
			new PayGateWayWapper().deleteCmbAggrementByUId("3");
			AcquireOrderBean aqb = PayGateWayCallService.initAcquireOrder("50");
			aqb.setUserId("3");
			PayGateWayCallService.acquireOrder(aqb);
			cmbsnotifyBean=PayGateWayCallService.initCMBSignNotifyBean(aqb);
			String sign=PayGateWayCallService.SignCMBNoticeData(cmbsnotifyBean.getNoticeData());
			cmbsnotifyBean.setSign(sign);
			String send = PayGateWayCallService.CMBNoticeSendString(cmbsnotifyBean);
//			alinotifyBean = initAliPayNotifyBean(aqb);
//			String send = aliPayNotifyBeanToSend(alinotifyBean,aqb);
			cmbsnotifyCall.setData(send);
			cmbsnotifyCall.addHeader("Mock", "1");
			String mockid = TestDataManager.getRandomUUID().toString();
			cmbsnotifyCall.addHeader("MockId", mockid);

			Logger.comment("添加Mock:" + mockid);
			cmbsnotifyCall.callService();
			Logger.debug(cmbsnotifyCall.getStatusCode());
			Logger.verifyEquals("success", cmbsnotifyCall.getReturnData(), "验证success");
			verifyDB(cmbsnotifyBean,"1");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	/**
	 * 验证数据
	 * @param cmbnb
	 * @param aggStatus 期望签约状态
	 * @throws InterruptedException
	 */
	private void verifyDB(CMBSignNotifyBean cmbnb,String aggStatus) throws InterruptedException {
		MCmbaggrement m=new PayGateWayWapper().selectCmbAggrementByUId(cmbnb.getNoticeData().getUserID()).get(0);
		Logger.debug(m);
		Logger.verifyEquals("0", m.getCancelType().toString(), "验证CancelType");
		Logger.verifyEquals(cmbnb.getNoticeData().getUserPidType(), m.getUserPidType(), "验证UserPidType");
		Logger.verifyEquals(cmbnb.getNoticeData().getNoPwdPay(), m.getNoPwdPay(), "验证NoPwdPay");
		Logger.verifyEquals(cmbnb.getNoticeData().getNoticeSerialNo(), m.getBankSerialNo(), "验证BankSerialNo");
		Logger.verifyEquals(cmbnb.getNoticeData().getUserID(), m.getUserId().toString(), "验证UserId");
		Logger.verifyEquals(cmbnb.getNoticeData().getDateTime(), m.getSignDateTime(), "验证SignDateTime");
		Logger.verifyEquals(cmbnb.getNoticeData().getAgrNo(), m.getId().toString(), "验证AggId");
		Logger.verifyEquals(cmbnb.getNoticeData().getUserPidHash(), m.getUserPidHash(), "验证UserPidHash");
		Logger.verifyEquals(aggStatus, m.getAggStatus().toString(), "验证AggStatus");
	}
}